package com.h3c.ptability.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author chenyifan
 * @date 2022/5/24 13:10
 */
@Getter
@AllArgsConstructor
public enum NeedSignEnum {

    /**
     * 不需要签署
     */
    UN_NEED_SIGN(0, "不需要签署"),

    /**
     * 需要签署
     */
    NEED_SIGN(1, "需要签署");

    @EnumValue
    @JsonValue
    private final Integer code;


    private final String statusName;

    /**
     * code 作为 key，封装为 Map
     */
    private static final Map<Integer, NeedSignEnum> CODE_MAP = Stream
            .of(NeedSignEnum.values())
            .collect(Collectors.toMap(NeedSignEnum::getCode, Function.identity()));

    /**
     * 静态工厂反序列化
     *
     * @param key code
     * @return 评论的类型枚举
     */
    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static NeedSignEnum valueOfKey(Integer key) {
        return Optional.ofNullable(CODE_MAP.get(key))
                .orElseThrow(() -> new IllegalArgumentException(String.valueOf(key)));
    }
}
